package com.mazong.serversecaa.service;

import com.mazong.serversecaa.IDbSecurityMapper;
import com.mazong.serversecaa.pojo.TbRole;
import com.mazong.serversecaa.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    IDbSecurityMapper iDbSecurityMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        TbUser user = iDbSecurityMapper.getUserByName(s);
        if(user != null) {
            List<TbRole> roles = iDbSecurityMapper.getRoleByUserId(user.getId());
            user.setAuthorities(roles);
        }

        return user;
    }
}
